import { Controller, Get, Post, Req, Res, UploadedFile, UseInterceptors } from '@nestjs/common';
import { UploadService } from './upload.service';
import { FileInterceptor } from '@nestjs/platform-express'
import { Express, Response } from 'express';
import { join } from 'path';
import {zip} from 'compressing'

@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}

  @Post('album')
  @UseInterceptors(FileInterceptor('file'))
  upload(@UploadedFile() file: Express.Multer.File) {
    console.log("file", file);
    return "123"
  }

  @Get('export')
  download(@Req() req: Request, @Res() res: Response) {
    const url = join(__dirname,'../images/1713399065014.jpg')
    res.download(url)
  }

  @Get('stream')
  async downloadWithStream(@Req() req: Request, @Res() res: Response){
    const url =  join(__dirname,'../images/1713399065014.jpg')
    const tarStream = new zip.Stream()
    await tarStream.addEntry(url)
    res.setHeader('Content-Type', 'application/octet-stream')
    res.setHeader(
      'Content-Disposition',
      'attachment; filename="mountain"'
    )
    tarStream.pipe(res)
    /**
     * 前端axios接收res = await fetch().then(res=>res.arrayBuffer())
     * blob = new Blob([res])
     * url = URL.createObjectURL(blob)
     * a = document.createElement('a')
     * a.href = Url
     */
  }

}
